#include "Triangle.hpp"
#include "rasterizer.hpp"
#include <eigen3/Eigen/Eigen>
#include <iostream>
#include <opencv2/opencv.hpp>

constexpr double MY_PI = 3.1415926;

Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos)
{
    Eigen::Matrix4f view = Eigen::Matrix4f::Identity();

    Eigen::Matrix4f translate;
    translate << 1, 0, 0, -eye_pos[0], 0, 1, 0, -eye_pos[1], 0, 0, 1,
        -eye_pos[2], 0, 0, 0, 1;

    view = translate * view;

    return view;
}

Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
    Eigen::Matrix4f model = Eigen::Matrix4f::Identity();

    // TODO: Implement this function
    // Create the model matrix for rotating the triangle around the Z axis.
    // Then return it.
	float const angle = rotation_angle * 0.01745329f;
	model << std::cos(angle), -std::sin(angle), 0, 0,
		std::sin(angle), std::cos(angle), 0, 0,
		0, 0, 1, 0, 
		0, 0, 0, 1;

    return model;
}

Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
                                      float zNear, float zFar)
{
    // Students will implement this function

    Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();

    // TODO: Implement this function
    // Create the projection matrix for the given parameters.
    // Then return it.

    float const fov = eye_fov * 0.01745329f;
	float w = 0.0f, h = 0.0f;
	h = 2 * zNear * std::tan(fov * 0.5f);
	w = h * aspect_ratio;
	float l = -w * 0.5f, r = w * 0.5f, t = h * 0.5f, b = -h * 0.5f;

    Eigen::Matrix4f m_scale, m_trans;
    Eigen::Matrix4f m_pers2ortho;
    m_scale << 2/(r-l), 0.f, 0.f, 0.f,
        0.f, 2/(t-b), 0.f, 0.f,
        0.f, 0.f, 2/(zNear - zFar), 0.f,
        0.f, 0.f, 0.f, 1.0f;
    m_trans << 1.0f, 0.f, 0.f, -(r + l) * 0.5f,
        0.0f, 1.0f, 0.0f, -(t + b) * 0.5f,
        0.0f, 0.0f, 1.0f, -(zNear + zFar) * 0.5f,
        0.0f, 0.0f, 0.0f, 1.0f;

    zNear = -zNear;
    zFar = -zFar;

	m_pers2ortho << zNear, 0.0f, 0.0f, 0.0f,
		0.0f, zNear, 0.0f, 0.0f,
		0.0f, 0.0f, zNear + zFar, -zNear * zFar,
		0.0f, 0.0f, 1.0f, 0.0f;
    projection = m_scale * m_trans * m_pers2ortho;
    projection.normalize();

    return projection;
}

Eigen::Matrix4f get_rotation(Eigen::Vector3f axis, float angle)
{
	Eigen::Matrix4f m = Eigen::Matrix4f::Identity();

	float angle_rad = angle * 0.01745329f;
	float sv = std::sin(angle_rad);
	float cv = std::cos(angle_rad);

	m << axis[0] * axis[0] * (1.0 - cv) + cv, axis[0] * axis[1] * (1 - cv) - axis[2] * sv, axis[0] * axis[1] * (1.0 - cv) + axis[1] * sv, 0.0f,
		axis[0] * axis[1] * (1 - cv) + axis[2] * sv, axis[1] * axis[1] * (1.0 - cv) + cv, axis[1] * axis[2] * (1.0 - cv) + axis[0] * sv, 0.0f,
		axis[0] * axis[1] * (1.0 - cv) - axis[1] * sv, axis[1] * axis[2] * (1.0 - cv) + axis[0] * sv, axis[2] * axis[2] * (1.0 - cv) + cv, 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f;

	return m;
}

int main(int argc, const char** argv)
{
    float angle = 0;
    bool command_line = false;
    std::string filename = "output.png";

    if (argc >= 3) {
        command_line = true;
        angle = std::stof(argv[2]); // -r by default
        if (argc == 4) {
            filename = std::string(argv[3]);
        }
        else
            return 0;
    }

    rst::rasterizer r(700, 700);

    Eigen::Vector3f eye_pos = {0, 0, 5.0};

    std::vector<Eigen::Vector3f> pos{{2, 0, -2}, {0, 2, -2}, {-2, 0, -2}};

    std::vector<Eigen::Vector3i> ind{{0, 1, 2}};

    auto pos_id = r.load_positions(pos);
    auto ind_id = r.load_indices(ind);

    int key = 0;
    int frame_count = 0;

    if (command_line) {
        r.clear(rst::Buffers::Color | rst::Buffers::Depth);

        r.set_model(get_model_matrix(angle));
        r.set_view(get_view_matrix(eye_pos));
        r.set_projection(get_projection_matrix(45, 1, 0.1, 50));

        r.draw(pos_id, ind_id, rst::Primitive::Triangle);
        cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());
        image.convertTo(image, CV_8UC3, 1.0f);

        cv::imwrite(filename, image);

        return 0;
    }

    while (key != 27) {
        r.clear(rst::Buffers::Color | rst::Buffers::Depth);

        r.set_model(get_model_matrix(angle));
		//r.set_model(get_rotation(Eigen::Vector3f(0.0f,0.0f,1.0f),angle));
        r.set_view(get_view_matrix(eye_pos));
        r.set_projection(get_projection_matrix(45, 1, 0.1, 50));

        r.draw(pos_id, ind_id, rst::Primitive::Triangle);

        cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());
        image.convertTo(image, CV_8UC3, 1.0f);
        cv::imshow("image", image);
        key = cv::waitKey(10);

        std::cout << "frame count: " << frame_count++ << '\n';

        if (key == 'a') {
            angle += 10;
        }
        else if (key == 'd') {
            angle -= 10;
        }
    }

    return 0;
}
